<template>
	<view class="page flex-col">
		<cu-custom bgColor="bg-gradual-pink" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">编辑资料</block>
		</cu-custom>
		<form>
			<view class="cu-form-group">
				<view class="title">头像</view>
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
						:data-url="$getFileAccessHttpUrl(imgList[index])">
						<image :src="$getFileAccessHttpUrl(imgList[index])" mode="aspectFill"></image>
						<view class="cu-tag bg-red radius" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
					
				</view>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">1111</view>
				<input placeholder="请输入姓名" disabled name="input" v-model="$getFileAccessHttpUrl(imgList[index])"></input>
			</view> -->
			<view class="cu-form-group">
				<view class="title">账号</view>
				<input placeholder="请输入姓名" disabled name="input" v-model="myFormData.account"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">用户名</view>
				<input placeholder="请输入用户名" name="input" v-model="myFormData.userName"></input>
			</view>


			<!-- <view class="cu-form-group">
				<view class="title">手机号</view>
				<input placeholder="输入手机号" name="input" v-model="myFormData.phone"></input>
				<view class="cu-capsule radius">
					<view class='cu-tag bg-blue '>
						+86
					</view>
					<view class="cu-tag line-blue">
						中国大陆
					</view>
				</view>
				<span class="span">{{rule.phone}}</span>
			</view>
			<view class="cu-form-group">
				<view class="title">电话号码</view>
				<input placeholder="输入电话号码" name="input" v-model="myFormData.telephone"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">邮箱</view>
				<input placeholder="输入邮箱" name="input" v-model="myFormData.email"></input>
				<span class="span">{{rule.email}}</span>
			</view> -->
			<view class="cu-form-group">
				<view class="title">公司名称</view>
				<input placeholder="输入邮箱" name="input" v-model="myFormData.company"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">税号</view>
				<input placeholder="输入税号" name="input" v-model="myFormData.taxNo"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">发票抬头</view>
				<input placeholder="输入发票抬头" name="input" v-model="myFormData.titleName"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">备注</view>
				<input placeholder="输入备注" name="input" v-model="myFormData.remark"></input>
			</view>
			<view class="padding flex flex-direction tong-fixed">
				<button class="cu-btn bg-blue lg" @click="onSubmit">提交</button>
			</view>
		</form>
	</view>
</template>

<script>
	import {
		USER_INFO
	} from "@/common/util/constants"
	import {
		encryption,
		decryption
	} from '@/common/util/aesEncrypt'
	import desensitizedUtils from '@/common/util/desensitized'
	import server from '@/api/server.js'
	export default {
		data() {
			return {
				index: -1,
				switchC: true,
				imgList: [],
				uploadUrl: "/sys/common/upload",
				rule: {
					email: '',
					phone: '',
				},
				myFormData: {
					account: '',
					phone: '',
					email: '',
					userName: 1,
					userType: '',
					telephoto: '',
					company: '',
					taxNo: '',
					titleName: '',
					remark: '',
				},
				encryptedString: {
					key: "",
					iv: "",
				},
			};
		},
		onShow() {
			this.loadinfo()
		},
		methods: {
			loadinfo() {
				let result = uni.getStorageSync(USER_INFO)
				console.log("用户", uni.getStorageSync(USER_INFO))
				server.getEncryptedString().then(res => {
					this.encryptedString.key = res.data.result.key
					this.encryptedString.iv = res.data.result.iv
					this.myFormData.phone = decryption(result.phone, res.data.result.key, res.data.result.iv)
					this.myFormData.email = decryption(result.email, res.data.result.key, res.data.result.iv)
				})
				this.imgList[0] = result.avatar
				this.myFormData.account = result.account
				this.myFormData.userName = result.userName
				this.myFormData.telephone = result.telephone == null ? '' : result.telephone
				this.myFormData.company = result.company
				this.myFormData.taxNo = result.taxNo == null ? '' : result.taxNo
				this.myFormData.titleName = result.titleName == null ? '' : result.titleName
				this.myFormData.remark = result.remark == null ? '' : result.remark

			},
			async onSubmit() {
				await this.desensitized()
				this.bidderedit()
			},
			desensitized(){
				//校验
				this.rule.phone = desensitizedUtils.checkField(this.myFormData.phone,'phone')
				this.rule.email = desensitizedUtils.checkField(this.myFormData.email,'email')
			},
				
			bidderedit(){
				let myForm = this.myFormData

				for (var key in this.rule) {
					if (this.rule[key] != '') return;
				}
				this.myFormData.phone = encryption(myForm.phone, this.encryptedString.key, this.encryptedString.iv)
				this.myFormData.email = encryption(myForm.email, this.encryptedString.key, this.encryptedString.iv)
				this.myFormData.id = uni.getStorageSync(USER_INFO).id
				this.myFormData.avatar = this.imgList[0]
				
				console.log('myform', this.myFormData)
				server.bidderedit(this.myFormData).then(res => {
					console.log(res)
					this.$tip.loaded();
					if (res.data.success) {
						uni.setStorageSync(USER_INFO, res.data.result);
						this.$tip.toast('编辑成功')
						// let pages = getCurrentPages()
						// let prevPage = pages[pages.length - 3]
						// prevPage.$vm.getStorage()	
						uni.navigateBack({})
					}
				}).catch(() => {
					this.$tip.loaded();
					this.$tip.error('提交失败')
				});
			},
			DateChange(e) {
				this.myFormData.birthday = e.detail.value
			},
			SwitchC(e) {
				this.switchC = e.detail.value
			},
			ChooseImage() {
				var that = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log(res)
						that.$http.upload(that.$config.apiUrl + that.uploadUrl + '?biz=' + 'temp', {
								filePath: res.tempFilePaths[0],
								name: 'file',
								
							}).then(res => {
								that.imgList[0] = res.data.message;
								console.log('res',that.imgList)
								this.$forceUpdate()
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						that.imgList = res.tempFilePaths
						
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	/deep/.cu-form-group {
		padding: 0 17px;
		min-height: 56px;
		position: relative;
	}

	/deep/.cu-form-group .span {
		position: absolute;
		bottom: 0;
		left: 100px;
		font-size: 12px;
		color: red;
	}
	/deep/.grid.grid-square {
		margin-top: 11px;
	}
</style>
